package com.leyou.controller;

import com.leyou.api.ActivityService;
import com.leyou.constant.DefineException;
import com.leyou.constant.ResponseMessage;
import com.leyou.constant.Result;
import com.leyou.domain.db1entity.Email;
import com.leyou.domain.db1entity.Manager;
import com.leyou.domain.vo.LoginVO;
import com.leyou.utils.Utils;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.rmi.CORBA.Util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@RequestMapping("login")
@RestController
public class LoginController {


    @Autowired
    ActivityService activityService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("loginCheck")
    public ResponseMessage loginCheck(Manager manager, HttpServletResponse response) {

        if (activityService.checkUser(manager) == 0) {
            return Result.error(-1, "用户名或密码错误");
        }

        manager.setTicket(Utils.getUUID(25));

        activityService.updateManagerTicket(manager);

        Cookie cookie = new Cookie("ticket", manager.getTicket());

        cookie.setPath("/");

        cookie.setMaxAge(1800);

        response.addCookie(cookie);

        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        valueOperations.set(manager.getTicket(), manager.getEmail(), 1800, TimeUnit.SECONDS);

        return Result.success();
    }

    @PostMapping("getCode")
    ResponseMessage getCode(Email email) {
        try {
            activityService.chekEmail(email);
        } catch (DefineException e) {
            return Result.error(e.getErrorCode(), e.getMessage());
        }

        email.setType("1");
        try {
            rabbitTemplate.convertAndSend("email", email);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @PostMapping("reg")
    ResponseMessage reg(Manager manager, HttpServletResponse response) {
        try {
            Email email = new Email();
            email.setTo(manager.getEmail());
            activityService.chekEmail(email);
        } catch (DefineException e) {
            return Result.error(e.getErrorCode(), e.getMessage());
        }

        if (!redisTemplate.hasKey(manager.getEmail())) {
            return Result.error(-1, "验证码不正确");
        }

        if (!manager.getManagerkey().equals(redisTemplate.opsForValue().get(manager.getEmail()))) {
            return Result.error(-1, "验证码不正确");
        }

        manager.setManagerkey(Utils.getUUID(32));
        manager.setManagername(manager.getEmail().split("@")[0]);
        manager.setTicket(Utils.getUUID(25));

        activityService.putManager(manager);

        Cookie cookie = new Cookie("ticket", manager.getTicket());

        cookie.setPath("/");

        cookie.setMaxAge(1800);

        response.addCookie(cookie);

        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

        valueOperations.set(manager.getTicket(), manager.getEmail(), 1800, TimeUnit.SECONDS);

        Email email = new Email();
        email.setTo(manager.getEmail());
        email.setType("2");
        rabbitTemplate.convertAndSend("email", email);

        return Result.success();
    }

    @PostMapping("getManagerInfoByTicket")
    ResponseMessage getManagerInfoByTicket(@CookieValue(name = "ticket") String ticket) {
        if (!redisTemplate.hasKey(ticket)) {
            return Result.error(-1, "登录失效");
        }

        Manager manager = activityService.getManagerByTicket(ticket);

        if (manager == null) {
            return Result.error(-1, "登录失效");
        }
        return Result.success(0, "成功", manager);
    }

    @PostMapping("find")
    ResponseMessage find(Manager manager) {

        if (!redisTemplate.hasKey(manager.getEmail())) {
            return Result.error(-1, "验证码不正确");
        }

        if (!manager.getManagerkey().equals(redisTemplate.opsForValue().get(manager.getEmail()))) {
            return Result.error(-1, "验证码不正确");
        }

        try {
            activityService.updateManager(manager);
        } catch (Exception e) {
            return Result.error(-1, "");
        }

        return Result.success(0, "修改成功，请登录");
    }

    @PostMapping("getCode2")
    ResponseMessage getCode2(Email email) {

        email.setType("3");
        try {
            rabbitTemplate.convertAndSend("email", email);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }
}
